﻿using CampusTrade.Pojo;
using CampusTrade.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConsultController : ControllerBase
    {
        private readonly ConsultService consultService;
        public ConsultController(ConsultService consultService)
        {
            this.consultService = consultService;
        }

        /**
         * 获取提问
         */
        [HttpGet("getQuestion/{goodId}")]
        public Dictionary<long, Question> getQuestion(long goodId)
        {
            List<Question> a = consultService.getQuestion(goodId);
            Dictionary<long, Question> dic = a.ToDictionary(Question => Question.GoodId);
            return dic;
        }
        /**
         * 上传提问
         */
        [HttpPost("addQuestion")]
        public long addComment(Question question)
        {
            question.CreateTime = (long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds;
            return consultService.addQuestion(question);
        }
        /**
         * 删除提问
         */
        [HttpDelete("delQuestion")]
        public int delQuestion(long id)
        {
            return consultService.deleteQuestion(id);
        }
        /**
         * 获取回复
         */
        [HttpGet("getAnswer/{goodId}")]
        public Dictionary<long, Answer> getAnswer(long questionId)
        {
            List<Answer> a = consultService.getAnswer(questionId);
            Dictionary<long, Answer> dic = a.ToDictionary(Answer => Answer.QuestionId);
            return dic;
        }
        /**
         * 上传回复
         */
        [HttpPost("addAnswer")]
        public long addAnswer(Answer answer)
        {
            answer.CreateTime = (long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds;
            return consultService.addAnswer(answer);
        }
        /**
         * 删除回复
         */
        [HttpDelete("delAnswer")]
        public int delAnswer(long id)
        {
            return consultService.deletAnswer(id);
        }
    }
}
